using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.IoC;
using ShopPowerTemplate.Presentation.Mvc.Controllers;
using ShopPowerTemplate.Presentation.Mvc.Extensions.BootStrapper;
using ShopPowerTemplate.Presentation.Mvc.ViewModels;
using ShopPowerTemplate.Services.Tests;
using System.Web.Mvc;

namespace ShopPowerTemplate.Presentation.Mvc.Test
{
    [TestClass]
    public class SubcategoryControllerTestBase
    {
        [TestInitialize]
        public virtual void TestInitialize()
        {
            IBootStrapper bootStrapper = new DefaultBootStrapper(IoCFactory.Instance.CurrentContainer);
            bootStrapper.Boot();
        }

        [TestMethod]
        public virtual void SubcategoryController_IndexTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());

            var result = controller.Index() as ViewResult;
            
            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is IndexSubcategoryViewModel);
        }

        [TestMethod]
        public virtual void SubcategoryController_LoadTest()
        {
            SubcategoryServiceUtil.CreateSubcategory();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());

            var result = controller.Load("", "", 0, 10, "") as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }

        [TestMethod]
        public virtual void SubcategoryController_DeleteTest()
        {
            Subcategory subcategory = SubcategoryServiceUtil.CreateSubcategory();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());

            var result = controller.Delete(subcategory.Id) as ContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(string.IsNullOrEmpty(result.Content));
        }

        [TestMethod]
        public virtual void SubcategoryController_ReportTest()
        {
            Subcategory subcategory = SubcategoryServiceUtil.CreateSubcategory();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());

            var result = controller.Report() as FileContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ContentType == "text/csv");
            Assert.IsNotNull(result.FileContents.Length > 0);
        }

        [TestMethod]
        public virtual void SubcategoryController_GetCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is CreateSubcategoryViewModel);
        }

        [TestMethod]
        public virtual void SubcategoryController_PostCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());
            Subcategory subcategory = SubcategoryServiceUtil.GetSubcategory();
            CreateSubcategoryViewModel viewModel = Mapper.Map<Subcategory, CreateSubcategoryViewModel>(subcategory);

            var result = controller.Create(viewModel) as RedirectToRouteResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.RouteValues["action"], "Edit"); //Create redirect to edit action on success
            Assert.IsTrue((int)result.RouteValues["id"] > 0); //Check new item id
        }

        [TestMethod]
        public virtual void SubcategoryController_GetEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());
            Subcategory subcategory = SubcategoryServiceUtil.CreateSubcategory();

            var result = controller.Edit(subcategory.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is EditSubcategoryViewModel);
            Assert.AreEqual((result.Model as EditSubcategoryViewModel).Name, subcategory.Name);
        }

        [TestMethod]
        public virtual void SubcategoryController_PostEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());
            Subcategory subcategory = SubcategoryServiceUtil.CreateSubcategory();
            subcategory.Name = "Test";

            EditSubcategoryViewModel viewModel = Mapper.Map<Subcategory, EditSubcategoryViewModel>(subcategory);
            controller.Edit(viewModel);

            var result = controller.Edit(subcategory.Id) as ViewResult;
            Assert.IsNotNull(result);
            Assert.AreEqual((result.Model as EditSubcategoryViewModel).Name, "Test");
        }

        [TestMethod]
        public virtual void SubcategoryController_DetailTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());
            Subcategory subcategory = SubcategoryServiceUtil.CreateSubcategory();

            var result = controller.Details(subcategory.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is DetailsSubcategoryViewModel);
            Assert.AreEqual((result.Model as DetailsSubcategoryViewModel).Name, subcategory.Name);
        }

		/* Details region test */
        [TestMethod]
        public virtual void SubcategoryController_LoadProductsTest()
        {
            Product products = ProductServiceUtil.CreateProductFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());

            var result = controller.LoadProducts("", "", 0, 10, "", 1, products.Subcategory.Id) as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }
        
        [TestMethod]
        public virtual void SubcategoryController_AddProductsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());
            Product products = ProductServiceUtil.GetProductFull();
            PopUpProductViewModel viewModel = Mapper.Map<Product, PopUpProductViewModel>(products);

            var result = controller.AddProducts(viewModel) as ContentResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content, "OK");
        }

        [TestMethod]
        public virtual void SubcategoryController_GetEditProductsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());
            Product products = ProductServiceUtil.CreateProductFull();

            var result = controller.EditProducts(products.Id, products.Subcategory.Id) as PartialViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.AreEqual(result.ViewName, "~/Views/Product/PopUp.cshtml");
            Assert.IsTrue(result.Model is PopUpProductViewModel);
			Assert.AreEqual((result.Model as PopUpProductViewModel).Name, products.Name);
            
        }

        [TestMethod]
        public virtual void SubcategoryController_PostEditProductsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());
            Product products = ProductServiceUtil.CreateProductFull();
            products.Name = "Test";

            PopUpProductViewModel viewModel = Mapper.Map<Product, PopUpProductViewModel>(products);
            controller.EditProducts(viewModel);

            var result = controller.EditProducts(products.Id) as PartialViewResult;
            Assert.IsNotNull(result);
			Assert.AreEqual((result.Model as PopUpProductViewModel).Name, "Test");
        }

        [TestMethod]
        public virtual void SubcategoryController_DeleteProductsTest()
        {
            Product products = ProductServiceUtil.CreateProductFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<SubcategoryController>());

            controller.DeleteProducts(products.Id, products.Subcategory.Id);

            Assert.AreEqual(controller.Response.StatusCode, 200);
        }

    }
}

